﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Web.Http;
using System.Web.Script.Serialization;
using ET.Monitor.Model;
using System.Web.Http.Cors;

namespace ET.Monitor.Web.Api
{
    public class RealTimeController : ApiController
    {

        [HttpGet]
        [EnableCors(origins: "*", headers: "*", methods: "*")]
        public HttpResponseMessage GetRTValueJson(string tag)
        {
            string[] tags = tag.Split(';');
            RealTimeModel rtm = new RealTimeModel(tags);
            double[] d = rtm.RetRTValue;
            for (int j = 0; j < d.Length; j++)
            {
                if (double.IsNaN(d[j]))
                    d[j] = -99999;
            }
            string json = "";
            Random r = new Random();
            for (int i = 0; i < tags.Length; i++)
            {
                json += "\"" + tags[i] + "\":" + d[i] + ",";//如果报错去掉数值引号转义
            }
            json = "{" + json.TrimEnd(',') + "}";
            //json = System.Web.HttpUtility.UrlEncode(json);
            HttpResponseMessage result = new HttpResponseMessage { Content = new StringContent(json, Encoding.GetEncoding("UTF-8"), "application/json") };
            return result;
        }

        [HttpGet]
        [EnableCors(origins: "*", headers: "*", methods: "*")]
        public HttpResponseMessage ExecuteAction(string code)
        {
            RealTimeModel rtm = new RealTimeModel(code);
            string ret = rtm.RetExecute;
            string json = "";

            json = "{\"state\":\"" + ret.ToString() + "服务器端执行成功\"}";
            HttpResponseMessage result = new HttpResponseMessage { Content = new StringContent(json, Encoding.GetEncoding("UTF-8"), "application/json") };
            return result;
        }
    }
}